<template>
	<view class="goods-list">
		<view v-if="type == 'waterfall'" class="goods-waterfall">
			<router-link v-for="(item, index) in list" :key="index"
				:to="{path: '/bundle/pages/goods_details/goods_details', query: {id: item.goods_id || item.id}}">
				<view :style="{width: width}" class="item bg-white m-t-20">
					<view class="goods-img">
						<u-image :width="width" :height="width" :border-radius="10" :src="item.image"></u-image>
					</view>
					<view class="goods-info" style="display: flex;justify-content: space-between;align-items: center;">
						<view>
							<view class="goods-name line-2">{{item.name}}</view>
							<view class="primary price mt10 row bg-menber-light">
								<text style="font-size: 22rpx;margin-right: 5rpx;">会员价</text>
								<price-format :color="colorConfig.primary" class="m-r-10" :first-size="34"
									:second-size="26" :subscript-size="26" :price="item.min_price"
									:weight="500"></price-format>
								<price-format class="muted" :firstSize="24" :secondSize="24" :subscript-size="24"
									line-through :price="item.market_price || item.price"></price-format>
							</view>
						</view>
						<image class="icon-lg" src="/static/images/icon_cart_add.png"
							@tap.stop="showSpecFun(1,item.id)">
						</image>
					</view>
				</view>
			</router-link>
		</view>
		<view v-if="type == 'double'" class="goods-double double flex flex-wrap row-between col-stretch p-l-20 p-r-20">
			<router-link class="m-t-20" v-for="(item, index) in list" :key="index"
				:to="{path: '/bundle/pages/goods_details/goods_details', query: {id: item.goods_id || item.id}}">
				<view class="item bg-white" :style="{width: width, height: '100%'}">
					<view class="goods-img">
						<u-image :width="width" :height="width" :border-radius="10" :src="item.image"></u-image>
					</view>
					<view class="goods-info" style="display: flex;justify-content: space-between;align-items: center;">
						<view>
							<view class="goods-name line-2" :class="{'store-tag': item.shop_type == 1}">{{item.name}}
							</view>
							<view class="primary price mt10 row bg-menber-light">
								<text style="font-size: 22rpx;margin-right: 5rpx;">会员价</text>
								<price-format :color="colorConfig.primary" class="m-r-10" :first-size="34"
									:second-size="26" :subscript-size="26"
									:price="item.activity_price || item.min_price" :weight="500"></price-format>
								<price-format class="muted" :firstSize="24" :secondSize="24" :subscript-size="24"
									line-through :price="item.market_price || item.price"></price-format>
							</view>
						</view>
						<image class="icon-lg" src="/static/images/icon_cart_add.png"
							@tap.stop="showSpecFun(1,item.id)">
						</image>
					</view>
				</view>
			</router-link>
		</view>
		<view v-if="type === 'one' && list.length" class="goods-one m-t-20">
			<router-link v-for="(item, index) in list" :key="index"
				:to="{path: '/bundle/pages/goods_details/goods_details', query: {id: item.goods_id || item.id}}">
				<view class="item bg-white flex col-top">
					<view class="goods-img">
						<u-image width="200rpx" height="200rpx" :border-radius="10" :src="item.image"></u-image>
					</view>
					<view class="goods-info m-l-20 flex-1">
						<view class="goods-name line-2 m-b-10" :class="{'store-tag': item.shop_type == 1}">{{item.name}}
						</view>
						<view class="flex row-between m-t-10">
							<view class="price m-t-10 flex bg-menber-light">
								<text style="font-size: 22rpx;margin-right: 5rpx;">会员价</text>
								<price-format :color="colorConfig.primary" class="m-r-10" :first-size="34"
									:second-size="26" :subscript-size="26" :price="item.min_price || item.price"
									:weight="500"></price-format>
								<price-format class="muted" :firstSize="24" :secondSize="24" :subscript-size="24"
									line-through :price="item.market_price || item.price"></price-format>
							</view>
						</view>
					</view>
				</view>
			</router-link>
		</view>

		<view v-if="type.includes('row')" class="goods-row flex">
			<router-link class="item" v-for="(item, index) in list" :key="index"
				:to="{path: '/bundle/pages/goods_details/goods_details', query: {id: item.goods_id || item.id}}">
				<view :class="[{'bg-white': showBg}]">
					<view class="goods-img">
						<u-image width="240rpx" height="240rpx" :border-radius="10" :src="item.image"></u-image>
					</view>
					<view class="goods-info">
						<view class="goods-name line-1 sm">{{item.name}}</view>
						<view class="primary price m-t-10 row bg-menber-light">
							<text style="font-size: 22rpx;margin-right: 5rpx;">会员价</text>
							<price-format :color="colorConfig.primary" class="m-r-10" :first-size="28" :second-size="22"
								:subscript-size="22" :price="item.min_price" :weight="500"></price-format>
							<price-format class="muted" :firstSize="22" :secondSize="22" :subscript-size="22"
								line-through :price="item.market_price || item.price"></price-format>
						</view>
					</view>
					<image v-if="index < 3 && type == 'row-hot'" class="paixu"
						:src="'/static/images/No.' + index + '.png'">
					</image>
				</view>
			</router-link>
		</view>
		<view v-if="type == 'new'" class="goods-new">
			<router-link v-for="(item, index) in list" :key="index"
				:to="{path: '/bundle/pages/goods_details/goods_details', query: {id: item.goods_id || item.id}}">
				<view class="item flex" :class="[{'bg-white': showBg}]">
					<view class="goods-img">
						<u-image width="214rpx" height="214rpx" :border-radius="10" :src="item.image"></u-image>
					</view>
					<view class="goods-info flex-1 m-l-20 flex-1">
						<view class="goods-name line-2">{{item.name}}</view>
						<view class="price m-t-20 flex row-between">
							<view class="muted xxs">原价<price-format :first-size="24" :second-size="24"
									:subscript-size="24" :price="item.market_price"></price-format>
							</view>
							<view class="muted xxs">{{item.sales_total}}人购买</view>
						</view>
						<view class="primary btn flex row-between m-t-20 bg-menber-light">
							<view class="flex">
								<text style="font-size: 22rpx;margin-right: 5rpx;">会员价&nbsp;</text>
								<price-format :color="colorConfig.primary" class="m-r-10" :first-size="34"
									:second-size="26" :subscript-size="26" :price="item.min_price"
									:weight="500"></price-format>
							</view>
							<button class="bg-primary br60 white btn" size="xs">去购买</button>
						</view>
					</view>
				</view>
			</router-link>
		</view>
		<view v-if="type == 'hot'" class="goods-hot">
			<router-link v-for="(item, index) in list" :key="index"
				:to="{path: '/bundle/pages/goods_details/goods_details', query: {id: item.goods_id || item.id}}">
				<view class="item flex bg-white m-t-20">
					<view class="goods-img">
						<u-image :lazy-load="true" width="180rpx" height="180rpx" border-radius="6rpx"
							:src="item.image" />
					</view>
					<view class="goods-info m-l-20 flex-1">
						<view class="goods-name line-2 m-b-10">{{item.name}}</view>
						<text class="sale br60 xxs">已有{{item.sales_total}}人购买</text>
						<view class="row-between flex m-t-10">
							<view class="price m-t-10 flex bg-menber-light">
								<text style="font-size: 22rpx;margin-right: 5rpx;">会员价&nbsp;</text>
								<price-format :color="colorConfig.primary" class="m-r-10" :first-size="34"
									:second-size="26" :subscript-size="26" :price="item.min_price" :weight="500">
								</price-format>
								<price-format class="muted" :firstSize="24" :secondSize="24" :subscript-size="24"
									line-through :price="item.market_price"></price-format>
							</view>
							<button class="bg-primary br60 white btn" size="xs">立即抢购</button>
						</view>
					</view>
					<image v-if="index < 3" class="paixu" :src="'/static/images/No.' + index + '.png'"></image>
				</view>
			</router-link>
		</view>
		<view v-if="type == 'activity'" class="goods-hot">
			<router-link v-for="(item, index) in list" :key="index"
				:to="{path: '/bundle/pages/goods_details/goods_details', query: {id: item.goods_id || item.id}}">
				<view class="item flex bg-white m-t-20">
					<view class="goods-img">
						<u-image :lazy-load="true" width="180rpx" height="180rpx" border-radius="6rpx"
							:src="item.image" />
					</view>
					<view class="goods-info m-l-20 flex-1">
						<view class="goods-name line-2 m-b-10">{{item.name}}</view>
						<text class="views br60 xxs">{{item.views}}浏览量</text>
						<view class="row-between flex m-t-10">
							<view class="price m-t-10 flex bg-menber-light">
								<text style="font-size: 22rpx;margin-right: 5rpx;">会员价&nbsp;</text>
								<price-format :color="colorConfig.primary" class="m-r-10" :first-size="34"
									:second-size="26" :subscript-size="26" :price="item.price" :weight="500">
								</price-format>
								<price-format class="muted" :firstSize="24" :secondSize="24" :subscript-size="24"
									line-through :price="item.market_price"></price-format>
							</view>
							<button class="bg-primary br60 white btn" size="xs">立即抢购</button>
						</view>
					</view>
				</view>
			</router-link>
		</view>

		<!-- 规格 --> 
		<spec-popup :show="showSpec" :goods="goodsDetail" :is-seckill="goodsType == 1" :type="goodsDetail.type"
			:show-stock="goodsDetail.is_show_stock == 1" :show-add="popupType == 1 || popupType == 0"
			:show-buy="popupType == 2 || popupType == 0" :show-confirm="popupType == 3" :red-btn-text="btnText.red"
			:yellow-btn-text="btnText.yellow" :group="!!isGroup" @close="showSpec = false" @buynow="onBuy"
			@addcart="onAddCart" @change="onChangeGoods" @confirm="onConfirm"></spec-popup>
	</view>
</template>

<script>
	import {
		mapActions,
		mapGetters
	} from "vuex";
	import {
		getGoodsDetail,
		addCart
	} from "@/api/store";
	import specPopup from "@/components/spec-popup/spec-popup.vue"
	export default {
		components: {
			specPopup
		},
		props: {
			type: {
				type: String,
				default: 'double'
			},
			list: {
				type: Array,
				default: []
			},
			isBargain: {
				type: Boolean,
				default: false
			},
			// 两行时有效
			width: {
				type: String,
				default: '347rpx'
			},
			showBg: {
				type: Boolean,
				default: true
			},
		},
		data() {
			return {
				isGroup: 0,
				popupType: "",
				showSpec: false,
				goodsType: 0,

				goodsDetail: {},
			};
		},
		computed: {
			btnText() {
				const {
					goodsType
				} = this;
				console.log("goodsType", goodsType);
				switch (goodsType) {
					case 1:
						return {
							red: "立即购买",
								yellow: "",
						};
					case 2:
						return {
							red: "立即开团",
								yellow: "单独购买",
						};
					default:
						return {
							red: "立即购买",
								yellow: "加入购物车",
						};
				}
			},
		},
		methods: {
			...mapActions(["getCartNum", "wxShare"]),
			async getGoodsDetailFun(id) {
				const {
					data,
					code
				} = await getGoodsDetail({
					goods_id: id,
				});
				console.log("detail", data);
				if (code == 1) {
					const {
						goods_image,
						content,
						comment,
						like,
						activity,
						shop,
						distribution,
					} = data;
					let {
						info,
						type,
						team,
						found,
						end_time
					} = activity; //秒杀时间
					// console.log("activity",activity);
					// 拼团的结束时间-现在的时间 = 剩余的时间
					let time = end_time ?
						end_time - Date.now() / 1000 :
						info ?
						info.activity_end_time - Date.now() / 1000 :
						0;

					if (found) {
						found = arraySlice(found, [], 2);
					}
					this.distribution = distribution || {};
					this.shop = shop;
					this.goodsDetail = data;
					this.swiperList = goods_image;
					this.comment = comment || {};
					this.countTime = time;
					this.goodsType = type;
					this.team = info ? info : {};
					this.teamFound = found ? found : [];

					this.$nextTick(() => {
						// 滚动到顶部，防止h5端出现问题
						uni.pageScrollTo({
							scrollTop: 0,
							duration: 0,
						});
						this.isFirstLoading = false;
						// this.getRectInfo();
					});
					// #ifdef H5
					const options = {
						shareTitle: this.team.share_title || data.name,
						shareImage: data.image,
						shareDesc: this.team.share_intro || data.remark,
					};
					this.wxShare(options);
					// #endif
				} else {
					this.isNull = true;
					this.isFirstLoading = false;
				}
			},

			// getRectInfo() {
			// 	if (this.topArr.length) return;
			// 	getRect("#goods").then((res) => {
			// 		this.topArr[0] = res.top - this.navHeight;
			// 	});
			// 	getRect("#evaluation").then((res) => {
			// 		this.topArr[1] = res.top - this.navHeight;
			// 	});
			// 	getRect("#details").then((res) => {
			// 		this.topArr[2] = res.top - this.navHeight;
			// 	});
			// },
			async showSpecFun(itemType, id) {
				await this.getGoodsDetailFun(id)
				console.log("执行")
				if (this.goodsType == 2 && [2, 3].includes(itemType)) {
					this.isGroup = 1;
				} else {
					this.isGroup = 0;
				}
				this.popupType = itemType;
				this.showSpec = true;
			},
			async onAddCart(e) {
				if (!this.isLogin) return this.$Router.push("/bundle_user/pages/login/login");

				if (this.goodsType == 2) {
					// 拼团单独购买
					const params = {
						goods: [{
							item_id: e.detail.id,
							num: e.detail.goodsNum,
							delivery_type: this.goodsDetail.default_delivery_type,
							goods_id: this.id,
							shop_id: this.shop.id,
						}, ],
						type: "buy",
						goodsType: this.goodsDetail.type,
					};
					this.$Router.push({
						path: "/bundle/pages/confirm_order/confirm_order",
						query: {
							data: params,
						},
					});
					return;
				}

				const {
					id,
					goodsNum
				} = e.detail;
				const {
					code,
					data,
					msg
				} = await addCart({
					item_id: id,
					goods_num: goodsNum,
				});
				if (code == 1) {
					this.getCartNum();
					this.$toast({
						title: msg,
						icon: "success",
					});
					this.showSpec = false;
				}
			},
			onBuy(e) {
				if (!this.isLogin) return this.$Router.push("/bundle_user/pages/login/login");
				const {
					id,
					goodsNum
				} = e.detail;
				const params = {
					goods: [{
						item_id: id,
						num: goodsNum,
						goods_id: this.id,
						shop_id: this.shop.id,
						delivery_type: this.goodsDetail.default_delivery_type,
					}, ],
					type: "buy",
					goodsType: this.goodsDetail.type,
				};
				this.showSpec = false;
				this.goodsType == 2 ? (params.teamId = this.team.id) : "";
				this.foundId ? (params.foundId = this.foundId) : "";
				console.log(params, 2222);
				console.log(this.checkedGoods, "this.checkedGoods");
				if (this.checkedGoods.price * params.goods[0].num < 20) return this.$toast({
					title: '订单金额未达到起送金额'
				});
				this.$Router.push({
					path: "/bundle/pages/confirm_order/confirm_order",
					query: {
						data: params,
					},
				});
			},
			onChangeGoods(e) {
				this.checkedGoods = e.detail;
			},
			onConfirm(e) {
				const {
					team: {
						team_id
					},
				} = this;

				teamCheck({
					team_id: this.foundId,
					// found_id: this.foundId,
					goods_id: e.detail.goods_id,
					item_id: e.detail.id,
					count: e.detail.goodsNum,
				}).then((res) => {
					if (res.code == 1) {
						this.onBuy(e);
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.goods-list {

		// 自营标签
		.store-tag::before {
			content: '自营';
			font-size: 22rpx;
			color: $-color-white;
			padding: 0 10rpx;
			background: linear-gradient(267deg, #FF2C3C 0%, #F52E99 100%);
			border-radius: 6rpx;
		}

		.goods-waterfall {
			.item {
				width: 347rpx;
				border-radius: 10rpx;
				overflow: hidden;

				.goods-info {
					padding: 10rpx;

				}
			}
		}

		.goods-double {
			.item {
				width: 347rpx;
				border-radius: 10rpx;
				overflow: hidden;

				.goods-info {
					padding: 10rpx;

					.goods-name {
						height: 80rpx;
						line-height: 40rpx;
					}

				}
			}
		}

		.goods-one .item {
			padding: 20rpx;

			&:not(:last-of-type) {
				margin-bottom: 20rpx;
			}
		}

		.goods-seckill .item {
			padding: 20rpx;
		}

		.goods-new .item {
			padding: 0 20rpx 20rpx;
			border-radius: 10rpx;
		}

		.goods-row {
			.item {
				position: relative;
				width: 240rpx;
				border-radius: 16rpx;
				overflow: hidden;

				&:not(:last-of-type) {
					margin-right: 20rpx;
				}

				.goods-info {
					padding: 10rpx;
				}
			}
		}

		.goods-hot {
			.item {
				position: relative;
				padding: 30rpx 20rpx;
				border-radius: 10rpx;

				.goods-info {
					width: 450rpx;

					.sale {
						padding: 4rpx 18rpx;
						color: #F79C0C;
						background-color: rgba(247, 156, 12, .1);
					}

					.views {
						padding: 4rpx 18rpx;
						color: #ff2c3c;
						background-color: rgba(237, 83, 73, .1);
					}
				}


			}
		}

		.goods-row,
		.goods-hot {
			.paixu {
				position: absolute;
				top: 0;
				left: 20rpx;
				width: 48rpx;
				height: 60rpx;
			}
		}
	}
</style>